package com.ks.service;

import com.ks.model.LogBean;
import com.ks.repository.LogRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.io.IOException;

@Service
public class LogService {
    @Autowired
    private LogRepo logRepo;

    @Transactional
    public void addLog1(String name){
        String txName = TransactionSynchronizationManager.getCurrentTransactionName();
        System.out.println("LogService,addLog1,txname="+txName);
        logRepo.save(LogBean.builder().content("log "+name).build());
    }
    @Transactional(propagation = Propagation.NOT_SUPPORTED)//没有事务
    public void addLog2(String name){
        String txName = TransactionSynchronizationManager.getCurrentTransactionName();
        System.out.println("LogService,addLog2,txname="+txName);
        logRepo.save(LogBean.builder().content("log "+name).build());
    }

    @Transactional
    public void addLog3(String name) throws IOException {
        String txName = TransactionSynchronizationManager.getCurrentTransactionName();
        System.out.println("LogService,addLog3,txname="+txName);
        logRepo.save(LogBean.builder().content("log "+name).build());
        throw new IOException("测试非运行时异常");
    }
    @Transactional
    public void addLog4(String name){
        String txName = TransactionSynchronizationManager.getCurrentTransactionName();
        System.out.println("LogService,addLog4,txname="+txName);
        logRepo.save(LogBean.builder().content("log "+name).build());
        int a=1/0;
    }
}
